<%--
  Created by IntelliJ IDEA.
  User: dhf
  Date: 2019/11/27
  Time: 9:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>淘美美-医院管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../static/css/admin.css" media="all" />
    <style>
        .layui-table-cell{
            height: 100%;
        }
    </style>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <input type="hidden" id="ids" value="${ids}">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">产品类目</label>
                    <div class="layui-input-block">
                        <select name="categoryId" id="categoryId">
                            <option value="0">全部</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit="" lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-hide" id="dataTables" lay-filter="dataTables"></table>
        </div>
    </div>
</div>
<script type="text/html" id="promoteToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="isAdd">确定</button>
        <button class="layui-btn layui-btn-sm" lay-event="isClose">关闭</button>
    </div>
</script>

<script src="../../static/lib/layui/layui.js"></script>
<script src="../../static/js/jquery.min.js"></script>
<script>
    var str = "";
    var aa = [];
    var bb = [];
    layui.config({
        base: "../../static/js/"
    }).extend({
        index: 'index' //主入口模块
    }).use(["index", "form", "laydate", "upload","table"], function (){
        var table = layui.table,form = layui.form;
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('dataTables', {
                where: {
                    name:field,
                }
            }, 'data');
        });
        //第一个实例
        table.render({
            elem: '#dataTables'
            ,url: '${path}/promote/getPromote/' //数据接口
            ,toolbar: '#promoteToolbar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '医院列表'
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                ,groups: 5 //只显示 1 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页
                ,theme:'#39aef5'
                ,limit:20
            }
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'id', title: '编号', width:90}
                ,{field: 'cover_pic', title: '产品集图片', width:120,templet: function(res){
                    return "<img src='"+res.cover_pic+"' style='width: 40px;height: 40px;' />"
                 }}
                ,{field: 'name', title: '产品名称', width:120}
                ,{field: 'inner_price', title: '价格', width:100}
                ,{field: 'reserve_num', title: '销量', width: 200}
            ]]
            ,done:function(res,page,count){
                var idx = $("#ids").val().replace("[","").replace("]","");
                str = idx;
                var arr= idx.split(",")
                aa = arr;
                for(var i = 0;i<res.data.length;i++){
                    for (var j = 0;j<arr.length;j++){
                        if(parseInt(res.data[i].id)==parseInt(arr[j])){
                            res.data[i]['LAY_CHECKED'] = 'true';
                            var index= res.data[i]['LAY_TABLE_INDEX'];
                            $('tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                            $('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                        }
                    }

                }
            }
        });
        //监听头部工具栏事件
        table.on('toolbar(dataTables)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data;  //获取选中行数据
            switch (obj.event){
                //确认选择事件
                case 'isAdd':
                    // var id = "";
                    // for(var i = 0;i<data.length;i++){
                    //     id += data[i].id + (i == data.length-1?"":",");
                    // }
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

                    //给父页面传值
                    parent.$('#promoteDiv').empty().append(' <button class="layui-btn" onclick="addAegin()" type="button" style="margin-left: 30px">继续选择</button>' +
                        '<input type="hidden" name="promoteGoods" id="promoteGoods" value="'+str+'"/>')
                    parent.layer.close(index);
                    return;
                case 'isClose':
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                    return ;
            }
        });
        layui.table.on('checkbox(dataTables)', function(obj) {
            if(obj.type == 'all') {
                var tableData = layui.table.cache.dataTables;
                if(obj.checked){
                    for(var i = 0;i<tableData.length;i++){
                        if(str!=""){
                            var b = aaC(str.split(","),tableData[i].id);
                            if(b){
                                if(str==""){
                                    str = tableData[i].id+""
                                }else{
                                    str = str+","+tableData[i].id
                                }
                            }
                        }else{
                            if(str==""){
                                str = tableData[i].id+""
                            }else{
                                str = str+","+tableData[i].id
                            }
                        }
                    }
                }else{
                    var str1 = str.split(",");
                    str = "";
                    for(var i = 0;i<str1.length;i++){
                        var b = aaD(tableData,str1[i]);
                        if(b){
                            if(str==""){
                                str = str1[i]+""
                            }else{
                                str = str+","+str1[i]
                            }
                        }

                    }
                }
                // 复选框全选切换
            } else if(obj.type == 'one') {
            // 单行复选框切换（当单行和全选同时 选中行数据解决采用行监听事件获取 ）
                if(obj.checked){
                    if(str!=""){
                        var b = aaC(str.split(","),obj.data.id)
                        if(b){
                            if(str==""){
                                str = obj.data.id+""
                            }else{
                                str = str+","+obj.data.id
                            }
                        }
                    }else{
                        if(str==""){
                            str = obj.data.id+""
                        }else{
                            str = str+","+obj.data.id
                        }
                    }
                }else{
                    var str1 = str.split(",");
                    str = "";
                    for (var i = 0 ; i<str1.length;i++){
                        if(parseInt(obj.data.id)!=parseInt(str1[i])){
                            if(str==""){
                                str = str1[i]+""
                            }else{
                                str = str+","+str1[i]
                            }
                        }
                    }
                }
            }
        });
        function aaC(str1,id){
            var b = true;
            for (var j = 0;j<str1.length;j++){
                if(parseInt(str1[j])==parseInt(id)){
                    console.log(str1[j]+","+id)
                    b = false;
                    break;
                }
            }
            return b;
        }

        function aaD(tableData,id){
            var b = true;
            for (var j = 0;j<tableData.length;j++){
                if(parseInt(tableData[j].id)==parseInt(id)){
                    console.log(tableData[j].id+","+id)
                    b = false;
                    break;
                }
            }
            return b;
        }
    });
</script>
</body>
</html>

